#include<bits/stdc++.h>
using namespace std;
int a,jq[2][6][65600],bh[2000][6],sc[2000];
string op,ad;
const char *qk[6]={"OK","FAIL","ERR"};
bool ps(char a){
	if(a<48||a>57) return 0;
	return 1;
}
int pj(char a){
	if(a=='C') return 0;
		else return 1;
}
bool pd(string a,int k){
	int numa=0,numb=0;
	int len1=a.size();
	bool same=0;
	int wz;
	if(!ps(a[0])) {
		cout<<'i';return 0;
	}
	for(int j=0;j<len1;j++){
		if(ps(a[j])){
			if(a[j]==48&&ps(a[j+1])) return 0;
			int num=0;
			while(ps(a[j])){
				num=num*10+int(a[j])-48;j++;
			}
			if(num>=256) {return 0;
			}
			numa++;
			bh[k][numa]=num;
			num=0;
		}
		else if(a[j]=='.'){
			numb++;
			if(numa==numb) continue;
			else {cout<<'.';return 0;
			}
		}
		else if(int(a[j])==58){
			if(numa==4&&numb==3) {
				wz=j+1;same=1;
			}
			else {cout<<':';return 0;
			}
			if(same) break;
		}
		else {
			return 0;
		}
	}
	int num5=0;
	for(int i=wz;i<len1;i++){
		if(ps(a[i])) num5=num5*10+int(a[i])-48;
		else {return 0;
		}
	}
	if(num5>=65536) return 0;
	bh[k][5]=num5;
	return 1;
}
int main(){
	freopen("network.in","r",stdin);
	freopen("network.out","w",stdout);
	cin>>a;
	for(int i=1;i<=a;i++) {
		cin>>op>>ad;
		bh[i][0]=pj(ad[0]);
		int len1=ad.size();
	//	for(int j=0;j<len1;j++) if(ad[j]==':') cout<<"OOO";
		pd(ad,i);
		bool same=false;
		if(pd(ad,i)){
			for(int j=1;j<i;j++){
				if(bh[i][0]){
					if(bh[i][1]==bh[j][1]&&bh[i][2]==bh[j][2]&&bh[i][3]==bh[j][3]&&bh[i][4]==bh[j][4]&&bh[i][5]==bh[j][5]){
						sc[i]=1;same=true;break;
					}
					
				}
				else{
					if(bh[j][0]&&bh[i][1]==bh[j][1]&&bh[i][2]==bh[j][2]&&bh[i][3]==bh[j][3]&&bh[i][4]==bh[j][4]&&bh[i][5]==bh[j][5]){
						sc[i]=j;same=true;break;
					}

				}
			}
			if(same&&bh[i][0]) sc[i]=-2;
			else if(same&&!bh[i][0]) sc[i]=-1;
			else continue;
		}
		else sc[i]=-3;
	}
	for(int i=1;i<=a;i++){
		if(sc[i]==-3) cout<"ERR";
		else if(sc[i]==-2) cout<<"OK";
		else if(sc[i]==-1) cout<<"FAIL";
		else cout<<sc[i];
		cout<<endl;
	}
	return 0;
}
